<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.name" placeholder="姓名"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="search">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="success" :disabled="this.sels.length===0" @click="onSale">上架</el-button>
                </el-form-item>
            </el-form>
        </el-col>
        <!--列表-->
        <el-table :data="dataList" highlight-current-row v-loading="listLoading"
                  @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="name" label="产品名字" width="120" sortable>
            </el-table-column>
            <el-table-column prop="state" label="状态" width="100" :formatter="formatSex" sortable>
            </el-table-column>
            <el-table-column prop="saleprice" label="售价" width="100" sortable>
            </el-table-column>
            <el-table-column prop="offsaletime" label="下架时间" width="180" sortable>
            </el-table-column>
            <el-table-column prop="onsaletime" label="上架时间" min-width="180" sortable>
            </el-table-column>
            <el-table-column prop="salecount" label="销量" min-width="100" sortable>
            </el-table-column>
            <el-table-column label="操作" width="150">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove"
                       :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next"
                           background
                           @current-change="handleCurrentChange"
                           :page-size="pageSize" :total="total" style="float:right;">
            </el-pagination>
        </el-col>


        <!-- Form -->
        <el-dialog title="产品维护" :visible.sync="dialogFormVisible">
            <el-form :model="product">
                <el-form-item label="产品名字" :label-width="formLabelWidth">
                    <el-input v-model="product.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="原价" :label-width="formLabelWidth">
                    <el-input v-model="product.costprice" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="折扣价" :label-width="formLabelWidth">
                    <el-input v-model="product.saleprice" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="产品图片" :label-width="formLabelWidth">
                    <el-upload
                            class="upload-demo"
                            action="/api/fastDfs/upload"
                            :on-remove="handleRemove"
                            :file-list="fileList"
                            :on-success="handleSuccess"
                            list-type="picture">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                    </el-upload>
                </el-form-item>
                <el-form-item label="产品详情" :label-width="formLabelWidth">
                   <!-- <el-input v-model="product.detail.intro" autocomplete="off"></el-input>-->
                    <quill-editor :options="quillConfig" v-model="product.detail.intro">

                    </quill-editor>
                </el-form-item>
                <el-form-item label="预约须知" :label-width="formLabelWidth">
                   <!-- <el-input v-model="product.detail.orderNotice" autocomplete="off"></el-input>-->
                    <quill-editor :options="quillConfig" v-model="product.detail.orderNotice">

                    </quill-editor>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitFormData">确 定</el-button>
            </div>
        </el-dialog>

    </section>

</template>

<script>
    /*富文本编辑器 样式*/
    import 'quill/dist/quill.core.css'
    import 'quill/dist/quill.snow.css'
    import 'quill/dist/quill.bubble.css'
    /*主要的js文件*/
    import { quillEditor } from 'vue-quill-editor'
    /*引入配置的js*/
    import quillConfig from '../../common/js/quill-config'

    export default {
        components:{
            quillEditor
        },
        data(){
            return {
                quillConfig:quillConfig,
                dataList:[],
                listLoading:false,//加载的动态效果
                sels:[],
                pageSize:5,
                total:0,
                currentPage:1,
                filters:{
                    name:''
                },
                dialogFormVisible:false,//默认隐藏弹出框
                formLabelWidth:"120px",
                product:{
                    name:'',
                    saleprice:null,
                    costprice:null,
                    resources:'',
                    state:0,
                    salecount:0,
                    detail:{
                        intro:'',
                        orderNotice:''
                    }

                },
                fileList: [
                ],


            }
        },

        methods:{
            //产品上架
            batchRemove(){



            },

            //文件上传成功的时候触发的方法
            handleSuccess(response, file, fileList){

                console.debug(fileList)
                let pathStr ='';

                for(var i =0 ;i<fileList.length;i++){
                  //  console.debug(fileList[i].response.resultObj)
                    //如果是最后一个 不加，
                   if(i==fileList.length-1){
                        pathStr+=fileList[i].response.resultObj;
                    }else { //非最后一个 拼接字符串添加一个，
                       pathStr+=fileList[i].response.resultObj+",";
                   }
                }
                //把文件上传成功的以后的路径赋值给logo
                this.product.resources=pathStr;


            },
            handleRemove(file, fileList) {
                // fileList 删完了 剩下的结果
                let pathStr ='';

                for(var i =0 ;i<fileList.length;i++){
                    //如果是最后一个 不加，
                    if(i==fileList.length-1){
                        pathStr+=fileList[i].response.resultObj;
                    }else { //非最后一个 拼接字符串添加一个，
                        pathStr+=fileList[i].response.resultObj+",";
                    }
                }
                //把文件上传成功的以后的路径赋值给logo
                this.product.resources=pathStr;
            },
            selsChange(sels){
                this.sels=sels;
            },
            formatSex(row, column){
                return row.state==1?'上架': '下架';
            },
            handleEdit(index,row){
                //回显数据
                this.product = Object.assign({}, row);
                //清空图片列表
                this.fileList=[];
                //给图片做回显
                if(row.resources){
                    let arr = row.resources.split(",");
                    for(let y =0;y<arr.length;y++){
                      //生成完整的url
                      let url="http://47.95.117.210:8888"+arr[y];
                        //生成对象
                      let obj =  { name:"图片"+(y+1),url:url,response:{resultObj:arr[y]}}
                      //把对象放到数组
                      this.fileList.push(obj);
                    }
                }
                //打开弹出框
                this.dialogFormVisible=true;
            },
            handleDel(index,row){
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    this.$http.delete("/product/"+row.id).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        if(res.data.success){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                        }else {
                            this.$message.error('网络异常');
                        }
                        this.loadListData();
                    });
                }).catch(() => {

                });
            },
            loadListData(){//加载页码数据
                let param={
                    currentPage:this.currentPage,
                    pageSize:this.pageSize,
                    keywords:this.filters.name
                }

                this.$http.post("/product",param).then(res=>{
                    console.debug(res)
                    this.dataList=res.data.rows;
                    this.total=res.data.tatal;
                })
            },
            onSale(){
                //获取删除数据的id的集合
                var ids = this.sels.map(item => item.id);// [1,2,4,3]

                this.$confirm('确认要上架产品?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(()=>{//确认按钮
                    //发送删除的请求
                    //ex: url /department/2
                    this.$http.patch("/product",ids).then(res=>{
                        //解构表达式
                        let {success,message}=res.data;
                        if (success){//删除成功
                            this.$message({
                                message: '操作成功',
                                type: 'success'
                            });
                        }else {//删除出现异常
                            this.$message.error('网络异常');
                        }
                        //重新加载列表数据
                        this.loadListData();
                    })
                }).catch(()=>{//取消的按钮
                    this.$message('已取消操作');
                })

            },
            //分页的方法
            handleCurrentChange(val){
                this.currentPage=val;
                this.loadListData();
            },
            search(){
                this.loadListData();
            },
            handleAdd(){
                //弹出框清空
                this.product={
                    name:'',
                    saleprice:null,
                    costprice:null,
                    resources:'',
                    state:0,
                    salecount:0,
                    detail:{
                        intro:'',
                        orderNotice:''
                    }
                }

                //清空图片列表
                this.fileList=[];
                //打开弹出框
                this.dialogFormVisible=true;

            },
            submitFormData(){//提交数据的方法

                this.$http.put("/product",this.product).then(res=>{
                    let {success,message}=res.data;
                    if(success){
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        });
                    }else {
                        this.$message.error('网络异常');
                    }
                    this.dialogFormVisible=false;
                    this.loadListData();

                })

            }
        },
        mounted(){
            this.loadListData();

        }


    }

</script>

<style scoped>

</style>